var normalelapse = 1000;
var nextelapse = normalelapse; 
var finish = "00:00:00";
var timer = null;


// 停止运行
function stop() {

	window.clearTimeout(timer);
}

// 倒计时函数
function onTimer()
{
	for(var i=0;i<=3;i++){
	var clock=document.getElementById("remainTime"+i);
	var start=clock.innerHTML;
	//alert("start : "+start);
	//alert("finish : "+finish);
	if (start == finish){
		//window.clearInterval(timer);
			//alert("time is up!");
		sendRequest();
		
		return;
	}
	
	var hms = new String(start).split(":");
	var s = new Number(hms[2]);
	var m = new Number(hms[1]);
	var h = new Number(hms[0]);


	s -= 1;
	if (s < 0)
	{
		s = 59;
		m -= 1;
	}

	if (m < 0)
	{
		m = 59;
		h -= 1;
	}
	
	var ss = s < 10 ? ("0" + s) : s;
	var sm = m < 10 ? ("0" + m) : m;
	var sh = h < 10 ? ("0" + h) : h;
	
	start = sh + ":" + sm + ":" + ss;
	clock.innerHTML = start;
	if((sh==0)&&(sm==0)&&(s<15)){
		
		sendRequest();
	}
	}
}


var count=1;
var request;
function createXMLRequest(){
	if(window.ActiveXObject){
		//alert("This is IE !");
		request=new ActiveXObject("Microsoft.XMLHTTP");
	}else if(window.XMLHttpRequest){
		//alert("This is FireFox !");
		request=new XMLHttpRequest();
	}else{
		//alert("你请求的对象不存在！");
	}
}


function processResult(){
        // alert("request:");
		if(request.readyState==4){
			if(request.status==200){
			    //   alert("request.readyState : ");
				callBack(request.responseText);
			}else { 
                //alert("Problem: " + request.statustext); 
            } 
		}
}

function sendRequest(){
	var auctionId=new Array();
	var idUrl="";
 	for(var i=0;i<4;i++){
 	//alert("here now !"+i);
		auctionId[i]=document.getElementById("auctioningId"+i).value;
		//alert("auctionId[i]:"+auctionId[i]);
		
		idUrl=idUrl+'auctionId'+i+'='+auctionId[i]+'&';
		
	//	alert("idUrl:"+idUrl);
	}
	var url='/duojinV1/updateTime.do?operation=updateTime&'+idUrl+'count='+count;
	createXMLRequest();
	request.onreadystatechange=processResult;
	request.open("GET",url,true);
	request.send(null);
	count++;	
}
function callBack(postData){
//alert("postData:"+postData);
	var data=new String(postData).split('|');
	var datas=new Array();
	for(var i=0;i<4;i++){
		//alert('there!!~~~');
		datas[i]=new String(data[i]);
		var post=new String(datas[i]).split("-");
		var j=new Number(post[1]);
		var remainTime=new String(post[0]);
		if(remainTime=='00:00:01'||remainTime=='00:00:00'){
			alert("有一个商品拍卖完成了！");
			window.location.reload(true);
		}else{
			var clock=document.getElementById("remainTime"+j);
			clock.innerHTML=remainTime;
		}
	}
	
	//alert("i:"+i);
	//alert("remainTime:"+remainTime);
	
	
}

//查询竞拍结果消息
function sendGetNewsRequest(){
   var url='/duojinV1/updateTime.do?operation=getNews';
    createXMLRequest();
	request.onreadystatechange=processGetNewsResult;
	request.open("GET",url,true);
	request.send(null);

}
 
function processGetNewsResult(){
 	if(request.readyState==4){
			if(request.status==200){
			 
				callBackGetNews(request.responseText);
			}else { 
               
            } 
		}
 }
 
 function callBackGetNews(backData){

 var data=new String(backData).split('|');
	   var datas=new Array();
		for(var i=0;i<4;i++){
	    var username=document.getElementById("username"+i);
	    var time=document.getElementById("time"+i);
	    var productname=document.getElementById("productname"+i);
	    var price=document.getElementById("price"+i);
	    var save=document.getElementById("save"+i);
		datas[i]=new String(data[i]);
		var post=new String(datas[i]).split(" ");
		username.innerHTML=post[0];
		time.innerHTML=post[1]; 
		productname.innerHTML=post[2];
		price.innerHTML=post[3]+"元";
		save.innerHTML=post[4];
	}
 
 
 }
